package com.iscas.tech.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.iscas.tech.entity.AtStpic;
import com.iscas.tech.mapper.AtStpicMapper;
import com.iscas.tech.param.AtStpicPageParam;
import com.iscas.tech.service.AtStpicService;
import com.iscas.tech.vo.AtStpicQueryVo;
import io.geekidea.springbootplus.framework.common.service.impl.BaseServiceImpl;
import io.geekidea.springbootplus.framework.pagination.PageUtil;
import io.geekidea.springbootplus.framework.pagination.Paging;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 科技成果图片信息表 服务实现类
 *
 * @author yangyi
 * @since 2020-10-26
 */
@Slf4j
@Service
public class AtStpicServiceImpl extends BaseServiceImpl<AtStpicMapper, AtStpic> implements AtStpicService {

    @Autowired
    private AtStpicMapper atStpicMapper;

    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean saveAtStpic(AtStpic atStpic) throws Exception {
        return super.save(atStpic);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean updateAtStpic(AtStpic atStpic) throws Exception {
        return super.updateById(atStpic);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean deleteTechPic(String ids) throws Exception {
        if (ids == null) {
            return true;
        }
        String[] idList = ids.split(",");
        for (String id : idList) {
            if (!atStpicMapper.deleteTechPic(Long.parseLong(id))) {
                throw new Exception();
            }
        }
        return true;
    }

    @Override
    public Paging<AtStpicQueryVo> getAtStpicPageList(AtStpicPageParam atStpicPageParam) throws Exception {
        Page page = PageUtil.getPage(atStpicPageParam, OrderItem.desc(getLambdaColumn(AtStpic::getCreatetime)));
        IPage<AtStpicQueryVo> iPage = atStpicMapper.getAtStpicPageList(page, atStpicPageParam);
        return new Paging(iPage);
    }

    @Override
    public List<AtStpicQueryVo> getByGainId(Integer techGainId, Integer techPicType) throws Exception {
        return atStpicMapper.getByGainId(techGainId, techPicType);
    }

    @Override
    public void deleteByGainId(Integer techGainId) throws Exception {
        atStpicMapper.deleteByGainId(techGainId);
    }


}
